package com.example.news.controller;

import com.example.news.pojo.form.ArticleForm;
import com.example.news.pojo.form.BasePageQuery;
import com.example.news.pojo.vo.ArticleDetailVo;
import com.example.news.pojo.vo.ArticleVo;
import com.example.news.service.ArticleService;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "5.新闻模块")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "保存新闻")
    public Result<String> createArticle(@RequestBody ArticleForm articleForm,@RequestHeader String token){
        return articleService.createArticle(articleForm,token);
    }

    @PostMapping("/modify")
    @Operation(summary = "修改新闻")
    public Result<String>modifyArticle(@RequestBody ArticleForm articleForm){
        return articleService.modifyArticle(articleForm);
    }

    //新闻发布
    @PostMapping("/publish")
    @Operation(summary = "发布新闻")
    public Result<String>publishArticle(@RequestBody ArticleForm articleForm){
        return articleService.publishArticle(articleForm);
    }

    //新闻删除
    @DeleteMapping("/delete/{articleId}")
    @Operation(summary = "删除新闻")
    public Result<String>deleteArticle(@PathVariable Integer articleId){
        return articleService.deleteArticleById(articleId);
    }

    //审核通过
    @PostMapping("/approve/{articleId}")
    @Operation(summary = "审核通过")
    public Result<String> approveArticle(@PathVariable Integer articleId) {
        return articleService.approveArticle(articleId);
    }

    //审核失败
    @PostMapping("/reject/{articleId}")
    @Operation(summary = "审核失败")
    public Result<String> rejectArticle(@PathVariable int articleId) {
        return articleService.rejectArticle(articleId);
    }

    //用户获取新闻列表
    @GetMapping("/user")
    @Operation(summary = "用户获取已发布的新闻列表")
    public PageResult<ArticleVo> userGetArticles(@ParameterObject BasePageQuery basePageQuery,Integer typeId){
            return articleService.userGetArticles(basePageQuery,typeId);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "id获取新闻详情")
    public Result<ArticleDetailVo> userGetArticles(@PathVariable Integer id){
        return articleService.getArticleById(id);
    }

    //管理员获取新闻列表
    @GetMapping("/admin")
    @Operation(summary = "管理员获取全部新闻列表")
    public PageResult<ArticleVo>adminGetArticles(@ParameterObject BasePageQuery basePageQuery){
            return articleService.adminGetArticles(basePageQuery);
    }
}

